import axios from 'axios'
// const tcb = require("tcb-js-sdk");
// const app = tcb.init({
//   env: 'start-ca1186'  // 此处填入您的环境ID
// })
// const auth = app.auth();
// let authHeader:any = null
// async function login(){
//   await auth.signInAnonymously();
//   // 匿名登录成功检测登录状态isAnonymous字段为true
//   const loginState = await auth.getLoginState();
//   // localStorage.setItem('access_token',loginState.credential.accessToken)
//   // console.log(loginState); // true
//   authHeader = tcb.auth().getAuthHeader()
//   console.log(authHeader)
// }

// login();
import {
  Toast
} from 'vant'
const service = axios.create({
  // baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // 跨域请求时发送cookies
  timeout: 5000 // 请求时间
})
// const urlRedirect = '/#/login'
service.interceptors.request.use( // 发送请求
  (config:any) => {
    config.headers['token'] = localStorage.getItem('token')
    // config.headers = { ...authHeader }
    // config.headers['access_token'] = localStorage.getItem('access_token')
    return config
  },
  (error: any) => {
    return Promise.reject(error)
  }
)
// 响应拦截器即异常处理
service.interceptors.response.use((response:any) => { // 响应
  // if(!response.data){
  //   return Toast.fail({
  //       message: '请先登录',
  //       onClose: () => {
  //         localStorage.removeItem('token')
  //         location.reload()
  //       }
  //     })
  // } else {
  //   return response.data
  // }
  const {
    success,
    errorCode,
    errorMsg
  } = response.data
  if (success === 'false' && errorCode === 'expired') {
    Toast.fail({
      message: errorMsg,
      onClose: () => {
        localStorage.removeItem('token')
        location.reload()
        // window.location.href = urlRedirect
        // console.log(urlRedirect)
      }
    })
  } else {
    return response.data
  }
}, (error: any) => {
  Toast('连接到服务器失败')
  return Promise.reject(error)
})

/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */
export function get (url: any, params:any = {}) {
  return new Promise((resolve, reject) => {
    service.get(url, {
      params
    })
      .then((response: any) => {
        if (response.success === 'true') {
          resolve(response)
        } else {
          Toast(response.errorMsg)
        }
      })
      .catch((err: any) => {
        Toast('服务器繁忙，请稍后再试')
        reject(err)
      })
  })
}
/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post (url: string, params:any = {}) {
  return new Promise((resolve, reject) => {
    if (url.indexOf('needLogin') !== -1) { // 这里测试写法
      params['token'] = localStorage.getItem('token')
    }
    service.post(url, params)
      .then((response: any) => {
        // resolve(response)
        if (response.success === 'true') {
          resolve(response)
        } else {
          Toast(response.errorMsg)
        }
      }, (err: any) => {
        Toast('服务器繁忙，请稍后再试')
        reject(err)
      })
  })
}
